package com.hzyb.shopOrder.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzyb.oldpack.dao.JdbcDao;
import com.hzyb.oldpack.dao.ShopFileDao;
import com.hzyb.shopCart.dao.ShopCartDao;
import com.hzyb.shopCart.model.ShopCart;
import com.hzyb.shopCommodity.dao.ShopCommodityDao;
import com.hzyb.shopCommodity.model.ShopCommodity;
import com.hzyb.shopOrder.dao.ShopOrderDao;
import com.hzyb.shopOrder.model.ShopOrder;
import com.hzyb.shopOrderdetail.dao.ShopOrderDetailDao;
import com.hzyb.shopOrderdetail.model.ShopOrderDetail;
@Service("shopOrderService")
@Transactional
public class ShopOrderServiceImpl implements com.hzyb.shopOrder.service.ShopOrderService{

	@Resource
	private ShopCartDao ShopCartDao;

	@Autowired
	private ShopOrderDao ShopOrderDao;

	@Autowired
	private ShopOrderDetailDao ShopOrderDetailDao;

	@Autowired
	private ShopCommodityDao ShopCommodityDao;

	@Autowired
	private JdbcDao JdbcDao;

	@Autowired
	private ShopFileDao ShopFileDao;

	@Override
	public List<Map<String, Object>> createShopOrderfromShopCart(String cartId) throws Exception {
		// TODO Auto-generated method stub
		String[] shopCarts=cartId.split(";");
		List<Map<String, Object>> order=new ArrayList<>();
		for(String cid:shopCarts) {
			ShopOrder shopOrder=new ShopOrder();
			ShopCart shopCart=ShopCartDao.findOne(cid);
			if(shopCart==null) {
				throw new Exception("cartId---->"+cid+" is not valid!!");
			}
			ShopCommodity shopCommodity=ShopCommodityDao.findOne(shopCart.getCommoditySid());
			shopOrder.setCommoditySid(shopCart.getCommoditySid());
			shopOrder.setNumber(String.valueOf(System.currentTimeMillis()));
			shopOrder.setEnterDate(new Date());
			shopOrder.setEnterUserid(shopCart.getEnterUserid());
			shopOrder.setUserid(shopCart.getUserid());
			shopOrder.setNum(shopCart.getNum());
			shopOrder.setState("0");
			shopOrder.setPrice(shopCommodity.getUnitPrice());
			shopOrder.setName(shopCommodity.getName());
			shopOrder.setDescription(shopCommodity.getDescription());
			shopOrder.setFreight(shopCommodity.getShippingCost());
			shopOrder.setFileId(shopCommodity.getFileId());
			ShopOrderDao.save(shopOrder);
			List<ShopOrderDetail> shopOrderDetails=ShopOrderDetailDao.findByOrderId(cid);
			for(ShopOrderDetail shopOrderDetail:shopOrderDetails) {
				ShopOrderDetail shopOrderDetail2=new ShopOrderDetail();
				shopOrderDetail2.setOrderId(shopOrder.getOrderSid());
				shopOrderDetail2.setSpecificationSid(shopOrderDetail.getSpecificationSid());
				shopOrderDetail2.setSpecificationValue(shopOrderDetail.getSpecificationValue());
				ShopOrderDetailDao.save(shopOrderDetail2);
			}
			ShopCartDao.delete(shopCart);
			Map<String, Object> map=new HashMap<>();
			map.put("shopOrder", shopOrder);
			map.put("shopCommodity", shopCommodity);
			map.putAll(JdbcDao.getImgBycommoditySid(shopCommodity.getCommoditySid()).get(0));
			order.add(map);
		}
		return order;

	}

	@Override
	public Map<String, Object> createShopOrder(JSONObject jsonObject) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> order=new HashMap();
		String commoditySid=jsonObject.getString("commoditySid");
		JSONArray type=jsonObject.getJSONArray("type");
		String num=jsonObject.getString("num");
		String openId=jsonObject.getString("openId");
		ShopCommodity shopCommodity=ShopCommodityDao.findOne(commoditySid);
		if(shopCommodity==null) {
			throw new Exception("commoditySid is not valid!!");
		}
		if(StringUtils.isEmpty(num)) {
			throw new Exception("num is not valid!!");
		}
		if(StringUtils.isEmpty(openId)) {
			throw new Exception("openId is not valid");
		}
		ShopOrder shopOrder=new ShopOrder();
		shopOrder.setCommoditySid(commoditySid);
		shopOrder.setEnterDate(new Date());
		shopOrder.setEnterUserid(openId);
		shopOrder.setNum(Integer.parseInt(num));
		shopOrder.setNumber(String.valueOf(System.currentTimeMillis()));
		shopOrder.setState("0");
		shopOrder.setUserid(openId);
		shopOrder.setFreight(shopCommodity.getShippingCost());
		shopOrder.setPrice(shopCommodity.getUnitPrice());
		shopOrder.setName(shopCommodity.getName());
		shopOrder.setDescription(shopCommodity.getDescription());
		shopOrder.setFileId(shopCommodity.getFileId());
		ShopOrderDao.save(shopOrder);
		for(Object o:type) {
			JSONObject shoptype=JSONObject.parseObject(String.valueOf(o));
			ShopOrderDetail shopOrderDetail=new ShopOrderDetail();
			shopOrderDetail.setOrderId(shopOrder.getOrderSid());
			shopOrderDetail.setSpecificationSid(shoptype.getString("id"));
			shopOrderDetail.setSpecificationValue(shoptype.getString("value"));
			ShopOrderDetailDao.save(shopOrderDetail);
		}
		order.put("shopOrder", shopOrder);
		order.put("shopCommodity", shopCommodity);
		order.putAll(JdbcDao.getImgBycommoditySid(shopCommodity.getCommoditySid()).get(0));
		return order;
	}

	@Override
	public Map<String, Object> queryOrderInformation(String orderId) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<>();
		ShopOrder shopOrder=ShopOrderDao.findOne(orderId);
		ShopCommodity shopCommodity=ShopCommodityDao.findOne(shopOrder.getCommoditySid());
		map.put("order", shopOrder);
		map.put("commodity", shopCommodity);
		map.put("type", JdbcDao.getShopType(orderId));
		map.put("img", ShopFileDao.findFilePathByFileId(shopCommodity.getFileId()));
		return map;
	}

	@Override
	public void orderCancel(String orderId) throws Exception {
		// TODO Auto-generated method stub
		ShopOrder shopOrder=ShopOrderDao.findOne(orderId);
		if(shopOrder.getState().equals("0")||shopOrder.getState().equals("1")) {
			shopOrder.setState("-1");
			ShopOrderDao.save(shopOrder);	
		}else {
			throw new Exception("Illegal Order Status!!");
		}
	}

	@Override
	public void remindShipment(String orderId) {
		// TODO Auto-generated method stub
		ShopOrder shopOrder=ShopOrderDao.findOne(orderId);
		Integer i=shopOrder.getRemindShipment()==null?0:shopOrder.getRemindShipment();
		shopOrder.setRemindShipment(i+1);
		ShopOrderDao.save(shopOrder);
	}

}
